package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.egg.ylt.R;
import com.egg.ylt.pojo.ActionVideoEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sichaoqi on 2018/9/5.
 */

public class ADA_VideoNews extends RecyclerView.Adapter{
    private Context mContext;
    private LayoutInflater mInflater;
    private List<ActionVideoEntity.ListBean> mList;
    private ADA_VideoImg imgAdapter;
    private OnItemClickListener mItemListener;

    public ADA_VideoNews(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
        imgAdapter = new ADA_VideoImg(mContext);
    }

    public void addList(List<ActionVideoEntity.ListBean> list){
        if (mList != null){
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void updateList(List<ActionVideoEntity.ListBean> list){
        mList.addAll(mList.size(), list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                return new OneViewHolder(mInflater.inflate(R.layout.item_consult_one, parent, false));
            case 1:
                return new TwoViewHolder(mInflater.inflate(R.layout.item_consult_two, parent, false));
            case 2:
                return new ThreeViewHolder(mInflater.inflate(R.layout.item_consult_three, parent, false));
            case 3:
                return new FourViewHolder(mInflater.inflate(R.layout.item_consult_four, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        final ActionVideoEntity.ListBean listBean = mList.get(position);

        if (holder instanceof OneViewHolder){
            //一张图片布局
            ((OneViewHolder)holder).itemOneTvDescription.setText(listBean.getTitle());
            Glide.with(mContext)
                    .load(listBean.getImageList().get(0).getImageUrl())
                    .placeholder(R.mipmap.ic_serve_default)
                    .into(((OneViewHolder)holder).itemOneIvImg);
        }else if (holder instanceof TwoViewHolder){
            //三张图片布局
            ((TwoViewHolder)holder).itemTwoDescription.setText(listBean.getTitle());
            //设置recycle布局管理器和适配器
            GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3);
            ((TwoViewHolder)holder).itemTwoRecyclerView.setLayoutManager(layoutManager);
            ((TwoViewHolder)holder).itemTwoRecyclerView.setAdapter(imgAdapter);
            //设置多张图片
            imgAdapter.addList(listBean.getImageList());
            ((TwoViewHolder)holder).itemTwoRecyclerView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return holder.itemView.onTouchEvent(event);
                }
            });
        }else if (holder instanceof ThreeViewHolder){
            //没有图片
            ((ThreeViewHolder)holder).itemThreeTvDescription.setText(listBean.getTitle());
            ((ThreeViewHolder)holder).itemThreeTvDetails.setText(listBean.getContent());

        }else if (holder instanceof FourViewHolder){
            //视频
            ((FourViewHolder)holder).itemFourTvDescription.setText(listBean.getTitle());
            Glide.with(mContext)
                    .load(listBean.getImageList().get(0).getImageUrl())
                    .placeholder(R.mipmap.ic_serve_default)
                    .into(((FourViewHolder)holder).itemFourIvImg);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemListener.onItemClick(listBean.getId(), listBean.getMessageStyle());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        String messageStyle = mList.get(position).getMessageStyle();
        switch (messageStyle){
            case "0"://只有文章，没有图片
                return 2;
            case "1"://一张图片
                return 0;
            case "2"://三张图片
                return 1;
            case "3"://视频
                return 3;
        }
        return super.getItemViewType(position);
    }

    class OneViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_one_tv_description)
        TextView itemOneTvDescription;
        @BindView(R.id.item_one_iv_img)
        ImageView itemOneIvImg;
        public OneViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class TwoViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_two_description)
        TextView itemTwoDescription;
        @BindView(R.id.item_two_recycler_view)
        RecyclerView itemTwoRecyclerView;
        public TwoViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ThreeViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_three_tv_description)
        TextView itemThreeTvDescription;
        @BindView(R.id.item_three_tv_details)
        TextView itemThreeTvDetails;
        public ThreeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class FourViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_four_tv_description)
        TextView itemFourTvDescription;
        @BindView(R.id.item_four_iv_img)
        ImageView itemFourIvImg;
        public FourViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(String id, String type);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.mItemListener = listener;
    }
}
